package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.TcDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchTcInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.ITcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TcServiceImpl implements ITcService {

    @Autowired
    private TcDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<TcInfo> selectPageListByQueryGetMap(Page<TcInfo> page, SearchTcInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcPhone())) {
            params.put("tcPhone", searchInfo.getTcPhone());
        }
        return dao.getQueryPage(page, params);
    }

    public TcInfo selectOneById(String tcId) {
        String sql = "select * from tc_info where id= '" + tcId + "'";
        TcInfo tcInfo = sqlMapper.selectOne(sql, TcInfo.class);
        return tcInfo;
    }

    public int insert(TcInfo info) {
        String id = CommUtils.getUUID();
        String sql = "";
        if (info.getTcBirthday().length() == 0) {
            sql = "INSERT INTO `tc_info` (`id`, `tc_name`, `tc_phone`, `tc_sex`, `tc_birthday`, `remarks`) VALUES" +
                    "('" + id + "','" + info.getTcName().trim() + "','" + info.getTcPhone().trim() + "'," + info.getTcSex() + "," + null + ",'" + info.getRemarks().trim() + "');";
        } else {
            sql = "INSERT INTO `tc_info` (`id`, `tc_name`, `tc_phone`, `tc_sex`, `tc_birthday`, `remarks`) VALUES" +
                    "('" + id + "','" + info.getTcName().trim() + "','" + info.getTcPhone().trim() + "'," + info.getTcSex() + ",'" + info.getTcBirthday() + "','" + info.getRemarks().trim() + "');";
        }
        return sqlMapper.insert(sql);
    }


    @Transactional
    public void delete(String id) {
        String sql = "delete from tc_info where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(TcInfo info) {
        if  (info.getTcBirthday().length() == 0) {
            info.setTcBirthday(null);
            dao.updateById(info);
            String sql = "update tc_info set tc_birthday="+null+" where id = '" + info.getId() + "';";
            return sqlMapper.update(sql);
        }else{
            return dao.updateById(info);
        }
    }

    public List<Map<String, Object>> list() {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        String sql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                " where b.abbreviation='"+tableHead+"' and (c.name ='Tc' or c.name ='Tc主管' or c.name ='Tc总监')";
        return sqlMapper.selectList(sql);
    }

    public List<Map<String, Object>> listAndDoctor() {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        String sql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                " where b.abbreviation='"+tableHead+"' " +
                " and (c.name like CONCAT('%','tc','%') or c.name='医生' or c.name='护士长' or c.name='洁牙师' or c.name='护士')  ";
        return sqlMapper.selectList(sql);
    }

    public List<Map<String, Object>> lists() {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        String sql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
        return sqlMapper.selectList(sql);
    }
}
